<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
$api = app('Dingo\Api\Routing\Router');

$api->version('v1', [
    'namespace' => 'App\Http\Controllers\Api',
    'middleware' => ['serializer:array', 'bindings'],
], function($api) {

    $api->group([
        'middleware' => 'api.throttle',
        'limit' => 99,
        'expires' => 1,
    ], function($api) {
        // 用户注册
        $api->post('register', 'AuthorizationsController@register')
            ->name('api.user.register');
        $api->post('login', 'AuthorizationsController@login')
            ->name('api.user.login');
        // 小程序用户登录
        $api->post('weapp/authorizations', 'AuthorizationsController@weappLogin')
            ->name('api.weapp_users.login');
        $api->post('weapp/register', 'AuthorizationsController@weappRegister')
            ->name('api.weapp_users.register');
        // 刷新token
        $api->put('authorizations/current', 'AuthorizationsController@update')
            ->name('api.authorizations.update');
        // 删除token
        $api->delete('authorizations/current', 'AuthorizationsController@destroy')
            ->name('api.authorizations.destroy');

        // 需要 token 验证的接口
        $api->group(['middleware' => 'api.auth'], function($api) {
            // 当前登录用户信息
            $api->get('user', 'UsersController@me')
                ->name('api.user.show');
            $api->post('watch/{movie}', 'WatchController@store')
                ->name('api.movies.watch');
            $api->delete('watch/{movie}', 'WatchController@destory')
                ->name('api.movies.unwatch');
            $api->put('watch/{movie}', 'WatchController@update')
                ->name('api.movies.watch.update');
            // 修改用户信息
            $api->patch('user', 'UsersController@update')
                ->name('api.user.patch');
            $api->put('user', 'UsersController@update')
                ->name('api.user.update');
            // 用户设置
            $api->get('user/setting', 'UserSettingsController@show')
                ->name('api.user.setting');
            $api->put('user/setting', 'UserSettingsController@update')
                ->name('api.user.setting');
            // 图片资源
            $api->post('images', 'ImagesController@store')
                ->name('api.images.store');
            //$api->resource('collects', 'CollectsController', ['only' => ['store', 'show', 'destory', 'index']]);

            $api->post('collects', 'CollectsController@store')
                ->name('api.collects.store');
            $api->put('collects/{collect}', 'CollectsController@update')
                ->name('api.collects.update');
            $api->delete('collects/{collect}', 'CollectsController@destroy')
                ->name('api.collects.destroy');

            $api->get('collects', 'CollectsController@index')
                ->name('api.collects.index');

            $api->delete('collects/{collect}/movies', 'CollectsController@removeMovies')
                ->name('api.collects.movies.delete');

            $api->post('collects/{collect}/movies', 'CollectsController@addMovies')
                ->name('api.collects.movies.store');
            // 关注 和 取消关注
            $api->post('followers/{user}', 'FollowersController@store')
                ->name('api.followers.create');
            $api->delete('followers/{user}', 'FollowersController@destroy')
                ->name('api.followes.destroy');
            // 消息
            $api->resource('notifications', 'NotificationsController', ['only' => ['index']]);
            // 未读消息数
            $api->get('notifications/stats', 'NotificationsController@stats');
            // 标记消息为已读
            $api->put('user/read/notifications', 'NotificationsController@read')
                ->name('api.user.notifications.read.put');
        });
        // 用户信息
        $api->get('users/{user}', 'UsersController@show')
            ->name('api.users.show')
            ->where('user', '[0-9]+');
        // 用户的电影列表
        $api->get('users/{user}/watched/movies', 'UsersController@watched')
            ->name('api.users.watched');

        $api->get('users/{user}/watching/movies', 'UsersController@watching')
            ->name('api.users.watching');

        $api->get('movies', 'MoviesController@index')
            ->name('api.movies.index');

        $api->get('movies/{movie}', 'MoviesController@show')
            ->where('movie', '[0-9]+')
            ->name('api.movies.show');
        $api->get('movies/search', 'MoviesController@search')
            ->name('api.movies.search');
        //全部评论
        $api->get('movies/comments', 'CommentsController@index')
            ->name('api.movies.comments');
        // 电影的评论
        $api->get('movies/comments/{movie}', 'CommentsController@movie')
            ->name('api.movies.comments.index');
        // 创建评论
        $api->post('movies/comments/{movie}', 'CommentsController@store')
            ->name('api.movies.comments.store');

        $api->get('categories', 'CategoriesController@index')
            ->name('api.categories.index');
        // 获取分类信息
        $api->get('categories/{category}', 'CategoriesController@show')
            ->name('api.categories.show');
        // 获取演员信息
        $api->get('actors/{actor}', 'ActorsController@show')
            ->name('api.actors.show');
        // 获取导演信息
        $api->get('directors/{director}', 'DirectorsController@show')
            ->name('api.directors.show');
        $api->get('authors/{author}', 'AuthorsController@show')
            ->name('api.authors.show');
        // 获取看单详情
        $api->get('collects/{collect}', 'CollectsController@show')
            ->name('api.collects.show');
        // 看单下的电影列表
        $api->get('collects/{collect}/movies', 'CollectsController@movies')
            ->name('api.collects.movies.index');
        // 某个用户的看单列表
        $api->get('users/{user}/collects', 'CollectsController@index')
            ->name('api.users.collects');
        // 用户的粉丝
        $api->get('followers', 'FollowersController@index')
            ->name('api.followers.index');
        // 打卡动态
        $api->get('explore', 'ExploreController@index');
    });
});
